package com.fpt.edu.vn.h2tv.api.staff;

import javax.ws.rs.DELETE;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.OPTIONS;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import com.fpt.edu.vn.h2tv.service.CategoryService;
import com.fpt.edu.vn.h2tv.service.IBaseService;

@Path("/manageCategory")
public class CategoryAdminAPI {
    
    @GET
    @Produces(MediaType.APPLICATION_XML)
    public Response getCategoryList(){
        IBaseService service = new CategoryService();
       return buildResponse(MediaType.APPLICATION_XML,service.getResponse());
    }
    
    @Path("/searchCategory")
    @GET
    @Produces(MediaType.APPLICATION_XML)
    public Response searchCategory(@QueryParam("txtCateName") String cateName){
        
        return buildResponse(MediaType.APPLICATION_XML, CategoryService.searchCategory(cateName));
    }
    
    @PUT
    @Produces(javax.ws.rs.core.MediaType.TEXT_HTML)
    public Response updateCategory(@QueryParam(value="txtIdCate") Short id, 
            @QueryParam("txtCateName") String cateName){
            
        return buildResponse(MediaType.TEXT_HTML, CategoryService.updateCategory(id, cateName));
    }
    
    @POST
    @Produces(javax.ws.rs.core.MediaType.TEXT_HTML)
    public Response addCategory(@FormParam(value="txtCateName") String cateName){
        
        return buildResponse(MediaType.TEXT_HTML, CategoryService.addCategory(cateName));
                
    }
    
    @DELETE
    @Produces(javax.ws.rs.core.MediaType.TEXT_HTML)
    public Response deleteCategory(@QueryParam(value="txtIdCate") Short id){
            
        return buildResponse(MediaType.TEXT_HTML, CategoryService.deleteCategory(id));
    }
    
    private Response buildResponse(String mediaType, Object repsonse){
        return Response.ok(repsonse, mediaType)
//                .header("Access-Control-Allow-Origin", "http://localhost")
//                .header("Access-Control-Allow-Methods", "GET,POST,PUT,DELETE,OPTIONS,TRACE,HEAD,CONNECT")
//                .header("Access-Control-Allow-Credentials",true)
//                .header("Access-Control-Allow-Headers", "x-accept-charset, x-accept, origin,X-Accept-Charset,X-Accept,Content-Type,Authorization,REFERER")
                .build();
    }
    
    @OPTIONS
    @Produces(javax.ws.rs.core.MediaType.TEXT_HTML)
    public Response test(){
            
        return buildResponse(MediaType.TEXT_HTML, "ok");
    }
}
